/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.service.impl;

import com.darren.euc.dao.ActivityDao;
import com.darren.euc.entity.Activity;
import com.darren.euc.entity.ActivityMemberRel;
import com.darren.euc.entity.PageBean;
import com.darren.euc.repository.ActivityMemberRelRepository;
import com.darren.euc.repository.ActivityRepository;
import com.darren.euc.service.ActivityService;
import com.darren.euc.util.DateUtil;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/11
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private ActivityRepository activityRepository;

    @Autowired
    private ActivityMemberRelRepository activityMemberRelRepository;

    @Override
    public PageBean queryForPageActivityHQL(JSONObject data, Integer pageSize, int currentPage) {
        String HQL = " from Activity where 1=1 and status > 0 order by is_top desc, begin_time desc ";
        return activityDao.queryForPageByHQL(HQL, pageSize, currentPage);
    }

    @Override
    public PageBean queryForPageActivityByMemberSQL(Integer memberId, Integer pageSize, int currentPage) {
        List<ActivityMemberRel> activityMemberRels = activityMemberRelRepository.findByMemberId(memberId.longValue());
        if (CollectionUtils.isEmpty(activityMemberRels)){
            return null;
        }
        List<Long> activityIds = activityMemberRels.stream().map(ActivityMemberRel::getActivityId).collect(Collectors.toList());
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC,"isTop"));
        orders.add(new Sort.Order(Sort.Direction.DESC,"beginTime"));
        Pageable pageable = new PageRequest(currentPage-1,pageSize,new Sort(orders));
        Page<Activity> activityPage = activityRepository.findByIdInAndStatusIn(activityIds,Lists.newArrayList(1,2,3),pageable);
        PageBean pageBean = new PageBean();
        pageBean.setList(activityPage.getContent());
        pageBean.setTotalPage(activityPage.getTotalPages());
        long totalElements = activityPage.getTotalElements();
        pageBean.setTotalRow(Long.valueOf(totalElements).intValue());
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.init();
        return pageBean;
    }

    @Override
    public PageBean queryForPageActivitySpaceHQL(List<Long> data, Integer pageSize, int currentPage) {
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC,"isTop"));
        orders.add(new Sort.Order(Sort.Direction.DESC,"beginTime"));
        Pageable pageable = new PageRequest(currentPage-1,pageSize,new Sort(orders));
        Page<Activity> activityPage = activityRepository.findByUserAreaIdInAndStatusIn(data,Lists.newArrayList(1,2,3),pageable);
        PageBean pageBean = new PageBean();
        pageBean.setList(activityPage.getContent());
        pageBean.setTotalPage(activityPage.getTotalPages());
        long totalElements = activityPage.getTotalElements();
        pageBean.setTotalRow(Long.valueOf(totalElements).intValue());
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.init();
        return pageBean;
    }

    @Override
    public Activity aoe(JSONObject data) {
        Long id = data.getLong("id");
        Activity activity = new Activity();
        if (id != null && id > 0 ){
            activity.setId(id);
            activity = activityRepository.findOne(id);
            activity.setModTime(new Date());
        } else {
            activity.setAddTime(new Date());
            activity.setModTime(new Date());
        }
        activity.setPersonName(data.getString("personName"));
        activity.setTitle(data.getString("title"));
        activity.setAddress(data.getString("address"));
        activity.setMaxCount(data.getInt("maxCount"));
        activity.setIsTop(data.getInt("isTop"));
        activity.setIsUnVip(data.getInt("isUnVip"));
        activity.setContent(data.getString("content"));
        activity.setUserAreaId(data.getLong("userAreaId"));
        activity.setUserArea(data.getString("userArea"));
        activity.setUrl(data.getString("url"));
        Date beginTime = DateUtil.string2Date(data.getString("beginTime"),"yyyy-MM-dd HH:mm:ss");
        Date endTime = DateUtil.string2Date(data.getString("endTime"),"yyyy-MM-dd HH:mm:ss");
        if (beginTime.after(endTime)){
            return null;
        }
        activity.setBeginTime(beginTime);
        activity.setEndTime(endTime);
        Date now = new Date();
        if (now.before(beginTime)){
            activity.setStatus(1);
        }else if (now.after(endTime)){
            activity.setStatus(3);
        }else{
            activity.setStatus(2);
        }
        return activityRepository.save(activity);
    }

    @Override
    public Activity findById(Long id) {
        return activityDao.findById(id);
    }

    @Override
    public Activity delete(Long id) {
        Activity activity = activityDao.findById(id);
        activity.setStatus(0);
        return activityDao.update(activity);
    }

    @Override
    public List<Activity> findByStatus(Integer status) {
        String HQL = " from Activity where status = ?";
        return activityDao.queryByCondition(HQL,status);
    }

    @Override
    public Activity update(Activity activity) {
        return activityDao.update(activity);
    }

    @Override
    public List<Activity> findNewTop5(Integer spaceId) {
        //return activityRepository.findTop5ByUserAreaIdInAndStatusInOrderByAddTimeDesc(Lists.newArrayList(spaceId.longValue(),0l), Lists.newArrayList(1,2,3));
    	String HQL ="";
    	if(spaceId==0){
        	HQL = " from Activity where is_un_vip=1 and status IN('1','2','3') order by is_top desc,id desc";
        }else{
        	HQL = " from Activity where user_area_id IN('0','"+ spaceId +"') and status IN('1','2','3') order by is_top desc,id desc";        	
        }

		return activityDao.queryByCondition(HQL);
    }
    @Override
    public List<Activity> findAllTop5() {
        String HQL = " from Activity where status IN('1','2') order by is_top desc,id desc";
        return activityDao.queryByCondition(HQL);
    }

    @Override
    public List<Activity> findByIdIn(List<Long> ids) {
        return activityRepository.findByIdIn(ids);
    }


}
